package com.dhcc.DRGsHosp.facade.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.inject.Named;

import net.sf.ehcache.CacheManager;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.ClinicGroupApplication;
import com.dhcc.DRGsHosp.application.CostSubApplication;
import com.dhcc.DRGsHosp.application.DRGVersionApplication;
import com.dhcc.DRGsHosp.application.DepartmentApplication;
import com.dhcc.DRGsHosp.application.DictionaryDataApplication;
import com.dhcc.DRGsHosp.application.FileUploadApplication;
import com.dhcc.DRGsHosp.application.HospInfoApplication;
import com.dhcc.DRGsHosp.application.WardApplication;
import com.dhcc.DRGsHosp.application.XmlStructureApplication;
import com.dhcc.DRGsHosp.core.domain.ClinicGroup;
import com.dhcc.DRGsHosp.core.domain.CostSub;
import com.dhcc.DRGsHosp.core.domain.Department;
import com.dhcc.DRGsHosp.core.domain.HospInfo;
import com.dhcc.DRGsHosp.core.domain.MrAppendPage;
import com.dhcc.DRGsHosp.core.domain.MrBaseInfo;
import com.dhcc.DRGsHosp.core.domain.MrDiagnose;
import com.dhcc.DRGsHosp.core.domain.MrExpense;
import com.dhcc.DRGsHosp.core.domain.MrExpenseSum;
import com.dhcc.DRGsHosp.core.domain.MrIpInfo;
import com.dhcc.DRGsHosp.core.domain.MrOperation;
import com.dhcc.DRGsHosp.core.domain.Ward;
import com.dhcc.DRGsHosp.core.domain.XmlStructure;
import com.dhcc.DRGsHosp.facade.DepGroupFacade;
import com.dhcc.DRGsHosp.facade.FileUploadFacade;
import com.dhcc.DRGsHosp.facade.XmlStructureFacade;
import com.dhcc.DRGsHosp.facade.dto.DepGroupDTO;
import com.dhcc.DRGsHosp.facade.dto.XmlStructureDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.XmlStructureAssembler;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;
import com.dhcc.DRGsHosp.infra.util.GlobalData;
import com.dhcc.DRGsHosp.infra.util.StringUtils;


/**
 * 
 * @title		:XmlStructureFacadeImpl.java
 * @description	:
 * @author		:mazhaoyan
 * @date		:2017年4月18日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014
 */
@Named("drgsHospFacadeImplMzy170418")
public class XmlStructureFacadeImpl implements XmlStructureFacade{
	private static final Logger logger = LoggerFactory.getLogger(XmlStructureFacadeImpl.class);
	
	@Autowired
	private XmlStructureApplication xmlStructureApplication;
	
	@Autowired
	private FileUploadApplication fileUploadApplication;
	
	@Autowired
	private FileUploadFacade fileUploadFacade;
	
    @Autowired
    private CostSubApplication costSubApplication;

    @Autowired
    private DepGroupFacade depGroupFacade ;

	@Autowired
	private DepartmentApplication  departmentApplication;
	
	@Autowired
	private WardApplication wardApplication;
	
	@Autowired
	private ClinicGroupApplication  clinicGroupApplication;
	
	@Autowired
	private DictionaryDataApplication  dictionaryDataApplication;
	
	@Autowired 
	private HospInfoApplication hospInfoApplication;
	
	@Autowired
	private DRGVersionApplication drgsVersionApplication;
	
	@Autowired
    private CacheManager cacheManager;
    
    private String jsonXmlStr;
    
    @Override
    public Map<String,Object> getInitProperty(){
    	Map<String,Object> initMap = new HashMap<String,Object>();
    	//查找费用表，构建map
		List<CostSub> costTemp = new ArrayList<CostSub>();// 用于存储首页表内的entity
		costTemp = costSubApplication.findAllCostSub();
		Map<String, CostSub> mapCostSub = new HashMap<String, CostSub>();
		for (int i = 0; i < costTemp.size(); i++) {
			String costSubCode = costTemp.get(i).getCostSubcode();
			mapCostSub.put(costSubCode, costTemp.get(i));
		}
		initMap.put("mapCostSub",mapCostSub);
		
		//院区
		List<HospInfo> hospList = new ArrayList<HospInfo>();
		hospList = hospInfoApplication.findAllHosp();
		initMap.put("hospList",hospList);

        // 科室组
        List<DepGroupDTO> depGroupDTOList = depGroupFacade.findAllDepGroup() ;
        Map<String, String> depGroupCodeMap = new HashMap<String, String>();//科室组map 科室组code->科室组名称
        for(DepGroupDTO depGroupDTO:depGroupDTOList){
            depGroupCodeMap.put(depGroupDTO.getDepGroupCode(),depGroupDTO.getDepGroupDesc()) ;
        }
        initMap.put("depGroupCodeMap",depGroupCodeMap);

		//所有科室
		List<Department> depList = new ArrayList<Department>();
		depList = departmentApplication.findAllDepartment();
		Map<String, String> depCodeMap = new HashMap<String, String>();//科室map 科室code->科室名称
		Map<String, String> depDescMap = new HashMap<String, String>();//科室map 科室名称->科室code
		Map<String, String> hospCodeMap = new HashMap<String, String>();//院区map 科室code->院区code
		for (Department dep : depList) {
			depCodeMap.put(dep.getDepCode(),dep.getDepName());
			depDescMap.put(dep.getDepName(),dep.getDepCode());
			hospCodeMap.put(dep.getDepCode(),dep.getParHospCode());
		}
		initMap.put("depCodeMap",depCodeMap);
		initMap.put("depDescMap",depDescMap);
		initMap.put("hospCodeMap",hospCodeMap);
		
		//所有病区
		List<Ward> wardList = new ArrayList<Ward>();
		wardList = wardApplication.findAllWard();
		Map<String, String> wardCodeMap = new HashMap<String, String>();//病区map 病区code->病区名称
		Map<String, String> wardDescMap = new HashMap<String, String>();//病区map 病区名称->病区code
		for (Ward ward : wardList) {
			wardCodeMap.put(ward.getWardCode(),ward.getWardDesc());
			wardDescMap.put(ward.getWardDesc(),ward.getWardCode());
		}
		initMap.put("wardCodeMap",wardCodeMap);
		initMap.put("wardDescMap",wardDescMap);
		
		//所有医师
		List<ClinicGroup> doctorList = new ArrayList<ClinicGroup>();
		doctorList = clinicGroupApplication.findAllClinicGroup();
		Map<String, String> docDescMap = new HashMap<String, String>();//医师map 医师名称->医师code
		Map<String, String> docCodeMap = new HashMap<String, String>();//医师map 医师code->医师名称
		//01:科主任 02:主任（副主任）医师  03:主治医师 04:住院医师 05:主诊医师 06:进修医师 07:研究生实习医师 08:实习医师 09:质控医师
		for (ClinicGroup cg : doctorList) {
			docDescMap.put(cg.getGroupName(),cg.getGroupCode());
			docCodeMap.put(cg.getGroupCode(),cg.getGroupName());
		}
		initMap.put("docCodeMap",docCodeMap);
		initMap.put("docDescMap",docDescMap);
		
		String autoGroup = GlobalData.sysConfig.get("AUTO_GROUP");//是否自动分组
		String groupVerCode = GlobalData.sysConfig.get("GROUP_VER");//分组器版本代码
		String groupVer=drgsVersionApplication.getDrgDescByVerCode(groupVerCode);//获取DRGs版本号
		String matchResult =GlobalData.sysConfig.get("ICD_MATCH");//诊断对照
		String groupAgain = GlobalData.sysConfig.get("GROUPINGAGAIN");//是否再分组
		String verRegion= drgsVersionApplication.getDrgRegionByVerCode(groupVerCode);
		String isICDMatch = "0";
		if("true".equals(matchResult)){
			isICDMatch = "1";
		}	
		String groupAddr = "";
		if(groupVer!=null&&groupVer.indexOf("北京2016版")>=0){
			groupAddr = GlobalData.sysConfig.get("GROUP_ADDER_2016");//分组器地址
		}else{
			groupAddr = GlobalData.sysConfig.get("GROUP_ADDER") ;//分组器地址
		}
		String icdOperVer = "";
		//手术操作编码版本
		String icdOperVerConfig = GlobalData.sysConfig.get("OPER_VERSION");//手术版本
		if(StringUtils.hasLength(icdOperVerConfig)){
			icdOperVer=icdOperVerConfig.contains("BJ")?"BJ":icdOperVerConfig.contains("CN")?"CN":"OT";
		}
		List<Map<String, Object>> dictionaryDataList = dictionaryDataApplication.findDictionaryDataByCode("");
		Map<String,Object> dictionaryDataMap = new HashMap<String,Object>();
		for(Map<String, Object> map:dictionaryDataList){
			String code=map.get("DICCODE")==null?"":map.get("DICCODE").toString();
			String desc=map.get("DICDESC")==null?"":map.get("DICDESC").toString();
			String value=map.get("DICVALUE")==null?"":map.get("DICVALUE").toString();
			dictionaryDataMap.put(code+"#"+desc, value);
		}
		initMap.put("autoGroup",autoGroup);
		initMap.put("groupVerCode",groupVerCode);
		initMap.put("groupVer",groupVer);
		initMap.put("matchResult",matchResult);
		initMap.put("isICDMatch",isICDMatch);
		initMap.put("groupAddr",groupAddr);
		initMap.put("icdOperVer",icdOperVer);
		initMap.put("groupAgain",groupAgain);
		initMap.put("verRegion",verRegion);
		initMap.put("dictData",dictionaryDataMap);
		//字段数据类型维护
		Map<String,String> dataTypeMap = new HashMap<String,String>();
        dataTypeMap.put("PMAININDEX", "String");
        dataTypeMap.put("P00010000", "String");
        dataTypeMap.put("P00010001", "String");
        dataTypeMap.put("P00020000", "String");
        dataTypeMap.put("P00020001", "String");
        dataTypeMap.put("P00030000", "String");
        dataTypeMap.put("P00040000", "Integer");
        dataTypeMap.put("P00050000", "String");
        dataTypeMap.put("P00060000", "String");
        dataTypeMap.put("P00070000", "String");
        dataTypeMap.put("P00070001", "String");
        dataTypeMap.put("P00080000", "Date");
        dataTypeMap.put("P00090000", "Integer");
        dataTypeMap.put("P00090001", "Integer");
        dataTypeMap.put("P00090002", "Integer");
        dataTypeMap.put("P00100000", "String");
        dataTypeMap.put("P00110000", "Integer");
        dataTypeMap.put("P00120000", "Integer");
        dataTypeMap.put("P00130000", "String");
        dataTypeMap.put("P00130100", "String");
        dataTypeMap.put("P00130200", "String");
        dataTypeMap.put("P00130300", "String");
        dataTypeMap.put("P00140000", "String");
        dataTypeMap.put("P00140100", "String");
        dataTypeMap.put("P00140200", "String");
        dataTypeMap.put("P00150000", "String");
        dataTypeMap.put("P00160000", "String");
        dataTypeMap.put("P00170000", "String");
        dataTypeMap.put("P00180000", "String");
        dataTypeMap.put("P00190000", "String");
        dataTypeMap.put("P00190100", "String");
        dataTypeMap.put("P00190200", "String");
        dataTypeMap.put("P00190300", "String");
        dataTypeMap.put("P00190400", "String");
        dataTypeMap.put("P00190500", "String");
        dataTypeMap.put("P00190600", "String");
        dataTypeMap.put("P00200000", "String");
        dataTypeMap.put("P00200100", "String");
        dataTypeMap.put("P00200200", "String");
        dataTypeMap.put("P00200300", "String");
        dataTypeMap.put("P00200400", "String");
        dataTypeMap.put("P00200500", "String");
        dataTypeMap.put("P00210000", "String");
        dataTypeMap.put("P00210100", "String");
        dataTypeMap.put("P00210200", "String");
        dataTypeMap.put("P00220000", "String");
        dataTypeMap.put("P00220100", "String");
        dataTypeMap.put("P00220200", "String");
        dataTypeMap.put("P00220300", "String");
        dataTypeMap.put("P00220400", "String");
        dataTypeMap.put("CASE_FLAG", "String");
        dataTypeMap.put("PUSH_DATE", "Date");
        dataTypeMap.put("P00230000", "String");
        dataTypeMap.put("P00230001", "String");
        dataTypeMap.put("P00240000", "String");
        dataTypeMap.put("P00240001", "String");
        dataTypeMap.put("P00250000", "Date");
        dataTypeMap.put("P00250100", "String");
        dataTypeMap.put("P00260000", "String");
        dataTypeMap.put("P00260001", "String");
        dataTypeMap.put("P00270000", "String");
        dataTypeMap.put("P00270001", "String");
        dataTypeMap.put("P00280000", "String");
        dataTypeMap.put("P00280001", "String");
        dataTypeMap.put("P00290000", "Date");
        dataTypeMap.put("P00290100", "String");
        dataTypeMap.put("P00300000", "String");
        dataTypeMap.put("P00300001", "String");
        dataTypeMap.put("P00310000", "String");
        dataTypeMap.put("P00310001", "String");
        dataTypeMap.put("P00320000", "Integer");
        dataTypeMap.put("P00330000", "String");
        dataTypeMap.put("P00330001", "String");
        dataTypeMap.put("P00340000", "String");
        dataTypeMap.put("P00350000", "String");
        dataTypeMap.put("P00360000", "String");
        dataTypeMap.put("P00370000", "String");
        dataTypeMap.put("P00380000", "String");
        dataTypeMap.put("P00380001", "String");
        dataTypeMap.put("P00390000", "Date");
        dataTypeMap.put("P00400100", "String");
        dataTypeMap.put("P00400200", "String");
        dataTypeMap.put("P00400300", "String");
        dataTypeMap.put("P00410000", "String");
        dataTypeMap.put("P00410001", "String");
        dataTypeMap.put("P00420000", "String");
        dataTypeMap.put("P00420001", "String");
        dataTypeMap.put("P00430000", "String");
        dataTypeMap.put("P00430001", "String");
        dataTypeMap.put("P00440000", "String");
        dataTypeMap.put("P00440001", "String");
        dataTypeMap.put("P00450000", "String");
        dataTypeMap.put("P00450001", "String");
        dataTypeMap.put("P00460000", "String");
        dataTypeMap.put("P00460001", "String");
        dataTypeMap.put("P00470000", "String");
        dataTypeMap.put("P00470001", "String");
        dataTypeMap.put("P00480000", "String");
        dataTypeMap.put("P00480001", "String");
        dataTypeMap.put("P00490000", "Integer");
        dataTypeMap.put("P00500000", "Integer");
        dataTypeMap.put("P00510000", "String");
        dataTypeMap.put("P00520000", "String");
        dataTypeMap.put("P00530000", "String");
        dataTypeMap.put("P00540000", "String");
        dataTypeMap.put("P00550000", "String");
        dataTypeMap.put("P00550101", "Integer");
        dataTypeMap.put("P00550102", "Integer");
        dataTypeMap.put("P00550103", "Integer");
        dataTypeMap.put("P00560000", "String");
        dataTypeMap.put("P00570000", "String");
        dataTypeMap.put("P00570001", "String");
        dataTypeMap.put("P00580000", "String");
        dataTypeMap.put("P00580001", "String");
        dataTypeMap.put("P00590000", "String");
        dataTypeMap.put("P00590100", "Double");
        dataTypeMap.put("P00590200", "Double");
        dataTypeMap.put("P00590300", "Double");
        dataTypeMap.put("P00590400", "Double");
        dataTypeMap.put("P00590500", "Double");
        dataTypeMap.put("P00600100", "String");
        dataTypeMap.put("P00600101", "String");
        dataTypeMap.put("P00600200", "String");
        dataTypeMap.put("P00600201", "String");
        dataTypeMap.put("P00600300", "String");
        dataTypeMap.put("P00600301", "String");
        dataTypeMap.put("P00601000", "String");
        dataTypeMap.put("P00601001", "String");
        dataTypeMap.put("P00600400", "String");
        dataTypeMap.put("P00600401", "String");
        dataTypeMap.put("P00600500", "String");
        dataTypeMap.put("P00600600", "String");
        dataTypeMap.put("P00600700", "String");
        dataTypeMap.put("P00600800", "String");
        dataTypeMap.put("P00600900", "String");
        dataTypeMap.put("P00610000", "String");
        dataTypeMap.put("P00610001", "String");
        dataTypeMap.put("P00610100", "String");
        dataTypeMap.put("P00610200", "String");
        dataTypeMap.put("P00610300", "Date");
        dataTypeMap.put("P00620000", "String");
        dataTypeMap.put("P00620001", "String");
        dataTypeMap.put("P00620100", "String");
        dataTypeMap.put("P00620200", "String");
        dataTypeMap.put("P00630000", "String");
        dataTypeMap.put("P00630001", "String");
        dataTypeMap.put("P00630100", "String");
        dataTypeMap.put("P00640001", "Integer");
        dataTypeMap.put("P00640002", "Integer");
        dataTypeMap.put("P00640003", "Integer");
        dataTypeMap.put("P00650001", "Integer");
        dataTypeMap.put("P00650002", "Integer");
        dataTypeMap.put("P00650003", "Integer");
        dataTypeMap.put("P00700000", "Integer");
        dataTypeMap.put("P00910000", "String");
        dataTypeMap.put("P00920000", "String");
        dataTypeMap.put("P00800000", "Double");
        dataTypeMap.put("P00810000", "Double");
        dataTypeMap.put("P00900000", "String");
        dataTypeMap.put("P00900001", "String");
        dataTypeMap.put("P00400101", "String");
        dataTypeMap.put("P00400201", "String");
        dataTypeMap.put("P00400301", "String");
        dataTypeMap.put("P00930000", "String");
        dataTypeMap.put("P00930001", "Date");
        dataTypeMap.put("P00930002", "Date");
        dataTypeMap.put("P00400401", "String");
        dataTypeMap.put("P00950000", "String");
        dataTypeMap.put("P00950001", "String");
        dataTypeMap.put("P00950002", "String");
        dataTypeMap.put("P00960000", "String");
        dataTypeMap.put("P00960001", "String");
        dataTypeMap.put("P00940001", "String");
        dataTypeMap.put("P00940002", "String");
        dataTypeMap.put("PDTYPE", "String");
        dataTypeMap.put("PDINDEX", "Integer");
        dataTypeMap.put("PD0100", "String");
        dataTypeMap.put("PD0101", "String");
        dataTypeMap.put("PD0200", "String");
        dataTypeMap.put("PD0201", "String");
        dataTypeMap.put("PD0300", "String");
        dataTypeMap.put("PD0301", "String");
        dataTypeMap.put("PD0202", "String");
        dataTypeMap.put("ID", "String");
        dataTypeMap.put("POTYPE", "String");
        dataTypeMap.put("POINDEX", "Integer");
        dataTypeMap.put("PO0100", "String");
        dataTypeMap.put("PO0101", "String");
        dataTypeMap.put("PO0200", "Date");
        dataTypeMap.put("PO0300", "String");
        dataTypeMap.put("PO0301", "String");
        dataTypeMap.put("PO0400", "String");
        dataTypeMap.put("PO0401", "String");
        dataTypeMap.put("PO0500", "String");
        dataTypeMap.put("PO0501", "String");
        dataTypeMap.put("PO0600", "String");
        dataTypeMap.put("PO0601", "String");
        dataTypeMap.put("PO0700", "String");
        dataTypeMap.put("PO0701", "String");
        dataTypeMap.put("PO0800", "String");
        dataTypeMap.put("PO0801", "String");
        dataTypeMap.put("PO0900", "String");
        dataTypeMap.put("PO0901", "String");
        dataTypeMap.put("PO1000", "String");
        dataTypeMap.put("PO1001", "String");
        dataTypeMap.put("PO1100", "Integer");
        dataTypeMap.put("PO1200", "String");
        dataTypeMap.put("PO1201", "String");
        dataTypeMap.put("PO1300", "String");
        dataTypeMap.put("PO1301", "String");
        dataTypeMap.put("PO1400", "String");
        dataTypeMap.put("PO1500", "Date");
        dataTypeMap.put("PO1501", "String");
        dataTypeMap.put("PO0201", "String");
        dataTypeMap.put("P90010000", "String");
        dataTypeMap.put("P90010001", "String");
        dataTypeMap.put("P90020000", "String");
        dataTypeMap.put("P90020001", "String");
        dataTypeMap.put("P90031000", "String");
        dataTypeMap.put("P90031001", "String");
        dataTypeMap.put("P90032000", "String");
        dataTypeMap.put("P90041000", "String");
        dataTypeMap.put("P90041001", "String");
        dataTypeMap.put("P90042000", "String");
        dataTypeMap.put("P90051000", "String");
        dataTypeMap.put("P90051001", "String");
        dataTypeMap.put("P90052000", "String");
        dataTypeMap.put("P90061000", "String");
        dataTypeMap.put("P90061001", "String");
        dataTypeMap.put("P90062000", "String");
        dataTypeMap.put("P90070000", "String");
        dataTypeMap.put("P90080000", "String");
        dataTypeMap.put("P90080001", "String");
        dataTypeMap.put("P90090000", "String");
        dataTypeMap.put("P90090001", "String");
        dataTypeMap.put("P90100000", "String");
        dataTypeMap.put("P90100001", "String");
        dataTypeMap.put("P90110000", "String");
        dataTypeMap.put("P90110001", "String");
        dataTypeMap.put("P90120000", "String");
        dataTypeMap.put("P90120001", "String");
        dataTypeMap.put("P90130000", "String");
        dataTypeMap.put("P90130001", "String");
        dataTypeMap.put("P90140000", "String");
        dataTypeMap.put("P90140001", "String");
        dataTypeMap.put("P90150000", "String");
        dataTypeMap.put("P90150001", "String");
        dataTypeMap.put("P90120201", "Double");
        dataTypeMap.put("P90160000", "Date");
        initMap.put("dataTypeMap",dataTypeMap);
    	return initMap;
    }

	@Override
	public List<Map<String,Object>> getTableStructure(String tableName) {
	    if("T_COSTSUB".equals(tableName)){//费用表
	       return  costSubApplication.getAllCostSub();
	    }
		return xmlStructureApplication.getTableStructure(tableName);
	}
	
	
	/**
	 * 
	 * <p>Title: readJson</p>   
	 * <p>Description: </p>   
	 * @return   
	 * @see com.dhcc.DRGsHosp.facade.XmlStructureFacade#readJson()
	 */
	@SuppressWarnings("unchecked")
    @Override
	public String readJson(String filePath) {
	    
	    //创建SAXReader对象  
        SAXReader reader = new SAXReader();  
        //读取文件 转换成Document  
        Document document;
        try {
            document = reader.read(new File(filePath));
          //获取根节点元素对象  
            Element root = document.getRootElement();
            jsonXmlStr = "[{id: 1,pId: 0,name: '"+root.getName()+"',open: true},";
            getElement(root.elements(),"");
            jsonXmlStr = jsonXmlStr.substring(0,jsonXmlStr.length()-1);
            jsonXmlStr += "]";
        } catch (DocumentException e) {
        	logger.error(e.getMessage());
            e.printStackTrace();
        }  
        finally {
			File tempFile = new File(filePath);
			if(tempFile.exists())  {  
				tempFile.delete();       
			}
		}
        return jsonXmlStr;
	}
	
	/**
     * 
     * @Title: creatXmlStructure
     * @author:LiQingYang   
     * @Description: 创建并保存xml结构对象   
     * @param: @param xmlStructureDTO      
     * @return: void      
     * @throws
     * @date:2017年4月26日 上午11:32:17
     */
    public String creatXmlStructure(XmlStructureDTO xmlStructureDTO){
    	String nodeId = xmlStructureDTO.getXpath() == null ? "" : (xmlStructureDTO.getXpath().hashCode()+"");
    	xmlStructureDTO.setNodeId(nodeId); 
        XmlStructure xmlStructure =  XmlStructureAssembler.toEntity(xmlStructureDTO);
        String state = "";
        try {
        	xmlStructureApplication.creatXmlStructure(xmlStructure);
		} catch (Exception e) {
			logger.error(e.getMessage());
			state = e.getMessage();
		}
        return state;
    }
    
    
    /**
     * 
     * <p>Title: nodeInfoRemove</p>   
     * <p>Description: 删除节点</p>   
     * @param treeId   
     * @see com.dhcc.DRGsHosp.facade.XmlStructureFacade#nodeInfoRemove(java.lang.String)
     */
    public void nodeInfoRemove(String nodeId){
    	if(nodeId!=null) {
    		nodeId = nodeId.hashCode()+"" ;
    	}
        int nodeInfoCount = xmlStructureApplication.getNodeInfoByNodeId(nodeId);
        if (nodeInfoCount > 0) {
            xmlStructureApplication.nodeInfoRemove(nodeId);
        }
    }
    

    /**
     * 
     * @Title: getElement
     * @author:LiQingYang   
     * @Description: 循环解析xml加载ztree   
     * @param: @param sonElemetList
     * @param: @param pId      
     * @return: void      
     * @throws
     * @date:2017年5月23日 上午10:11:12
     */
    @SuppressWarnings("unchecked")
    private void getElement(List<Element> sonElemetList,String pId) {  
        for (Element sonElement : sonElemetList) {
            String jsonNodeStr = "{";
            String nodeTreeId = StringUtils.getRandomNumberString(10);
            jsonNodeStr += "id:"+nodeTreeId+",";
            jsonNodeStr += "pId:"+(pId.isEmpty() ? 1 : pId)+",";
            jsonNodeStr += "name: '"+sonElement.getName()+"'";
            if (sonElement.elements().size() != 0) { //有子节点继续钻取加载ztree
                jsonNodeStr += ",open:true}";
                jsonXmlStr += jsonNodeStr.toString()+",";
                getElement(sonElement.elements(),nodeTreeId);  
            }else {
                jsonNodeStr += "}";
                jsonXmlStr += jsonNodeStr.toString()+",";
            }
        }
    }
    /**
     * 
     * @Title: getXmlInfoByStructure
     * @author:LiQingYang   
     * @Description: 获取xml数据   
     * @param:       
     * @return: void      
     * @throws
     * @date:2017年4月26日 下午4:20:08
     */
    @SuppressWarnings("unchecked")
	public String getXmlInfoByStructure(String filePath){
    	GlobalData.fileInfo = "正在解析文件……";
    	try {
	    	//创建SAXReader对象  
	    	SAXReader reader = new SAXReader();  
	    	//读取文件 转换成Document
	    	Document document = reader.read(new File(filePath));
	    	//获取根节点元素对象  
			Element root = document.getRootElement();
			String rootName = root.getName();//根节点名称
			List<Object[]> xmlStrucList = xmlStructureApplication.getXmlStructureByParam("medicalRecord","","","");
			String caseNodeName = "";//单病例节点的名称
			Map<String,Object> initMap = getInitProperty();
			if(!xmlStrucList.isEmpty())
			{
				caseNodeName = xmlStrucList.get(0)[3].toString();
				if(caseNodeName.startsWith("/"+rootName))
				{
					initMap.put("pathPrefix","");
				}
				else {
					initMap.put("pathPrefix","/"+rootName);
				}
				
				List<Node> nodeChild = root.selectNodes("/"+rootName+"/"+caseNodeName.split("/")[1]);
				for(int i = 1;i<=nodeChild.size();i++)
				{
					initMap.put("curNodeName",nodeChild.get(i-1).getName());
					initMap.put("num","["+i+"]");
					parseXmlNode(root,initMap);
				}
				cacheManager.clearAll();
			}
    	}
    	catch(Exception e)
    	{	
    		logger.error(e.getMessage());
    		return e.getMessage();
    	}
    	finally {
    		File tempFile = new File(filePath);
			if(tempFile.exists())  {  
				tempFile.delete();       
			}
		}
    	return "success";
    }
    @SuppressWarnings("unchecked")
	public void parseXmlNode(Element element,Map<String,Object> paramMap)
    {
    	Map<String,Object> caseMap = new HashMap<String,Object>();//病例信息map
    	String pathPrefix = paramMap.get("pathPrefix").toString();
    	String curNodeName = paramMap.get("curNodeName").toString();
    	String num = paramMap.get("num").toString();
		// 基本信息
		MrBaseInfo mrBaseInfo = new MrBaseInfo();
		// 住院信息
		MrIpInfo mrIpInfo = new MrIpInfo();
		// 附页基本信息
		MrAppendPage mrAppendInfo = new MrAppendPage();
		// 费用和信息
		MrExpenseSum expenseSum = new MrExpenseSum();
		//诊断list
		List<MrDiagnose> diagList = new ArrayList<MrDiagnose>();
		//手术list
    	List<MrOperation> operList = new ArrayList<MrOperation>();
    	//费用list
    	List<MrExpense> expList = new ArrayList<MrExpense>();
    	

		String pMainIndex = "";// 基本信息表主键
		String mrNo = "";// 病案号
		String mrOutDate = "";// 出院日期
		String mrInHospCount = "";// 住院次数
		String xmlPathInfo = "";//记录当前解析的xmlPath
		// dic_code#dic_value : dic_desc  键值对
		List<Map<String, Object>> dictionaryDataList = dictionaryDataApplication.findDictionaryDataByCode("");
		Map<String,Object> dictionaryDataMap = new HashMap<String,Object>();
		for(Map<String, Object> map:dictionaryDataList){
			String code=map.get("DICCODE")==null?"":map.get("DICCODE").toString();
			String desc=map.get("DICDESC")==null?"":map.get("DICDESC").toString();
			String value=map.get("DICVALUE")==null?"":map.get("DICVALUE").toString();
			dictionaryDataMap.put(code+"#"+desc, value);
		}
		
		try {
			// 基本信息
			List<Object[]> baseInfoXmlStrucList = xmlStructureApplication.getXmlStructureByParam("medicalRecord","MR_BASEINFO", "", "");
			if (!baseInfoXmlStrucList.isEmpty()) {
				Map<String,Object> filedMap = new HashMap<String,Object>();
				filedMap.put("PMAININDEX","pmainIndex");
				for (Object[] objects : baseInfoXmlStrucList) {
					// 获取基本信息xml路径
					String xpathBaseInfo = pathPrefix + objects[3].toString().replaceFirst(curNodeName,curNodeName+num);
					xmlPathInfo = xpathBaseInfo;
					System.out.println("基本信息xml路径为：" + xpathBaseInfo);
					Node nodeBi = element.selectSingleNode(xpathBaseInfo);
					if(nodeBi==null)
					{
						continue;//当前节点不存在 跳过
					}
					System.out.println(nodeBi.getName() + "==========" + nodeBi.getText() + "=====Table====" + (String) objects[10] + "======Filed=====" + (String) objects[12]);
					String nodeTextVal = nodeBi.getText().toString();
					// 年龄(去除中文字符)
					if ("P00090000".equals((String) objects[12])) {
						nodeTextVal = nodeTextVal.replaceAll("[\\u4e00-\\u9fa5]", "");
					}
					// 使用反射，调用对象实体类Set方法
					// 判断字符串是否为空串 空串直接跳过 不处理
					if("".equals(nodeTextVal))
					{
						continue;//跳过本次
					}
					DrgsIndex.setElementValue(mrBaseInfo, objects[4].toString(),objects[13] == null ? "" : objects[13].toString(), objects[12].toString(), nodeTextVal,objects[5] == null ? "" : objects[5].toString(),filedMap);
				}
				if (mrBaseInfo.getP00020001() == null && mrBaseInfo.getP00020000() != null) {// 付费方式
					String payWay = mrBaseInfo.getP00020000();// 医疗付费方式
					payWay =  dictionaryDataMap.get("P00020001"+"#"+payWay) == null ? null : dictionaryDataMap.get("P00020001"+"#"+payWay).toString() ;
					mrBaseInfo.setP00020001(payWay);
				}
				if (mrBaseInfo.getP00070001() == null && mrBaseInfo.getP00070000() != null) {// 性别
					String gender = mrBaseInfo.getP00070000();// 性别
					gender =  dictionaryDataMap.get("P00070000"+"#"+gender) == null ? null : dictionaryDataMap.get("P00070000"+"#"+gender).toString() ;
					mrBaseInfo.setP00070001(gender);
				}
			}

			// 住院信息
			List<Object[]> ipInfoXmlStrucList = xmlStructureApplication.getXmlStructureByParam("medicalRecord","MR_IPINFO", "", "");
			if (!ipInfoXmlStrucList.isEmpty()) {
				for (Object[] objects : ipInfoXmlStrucList) {
					String xpathIp = pathPrefix + objects[3].toString().replaceFirst(curNodeName,curNodeName+num);
					xmlPathInfo = xpathIp;
					System.out.println("住院信息xml路径为：" + xpathIp);
					Node nodeIp = element.selectSingleNode(xpathIp);
					if(nodeIp==null)
					{
						continue;//当前节点不存在 跳过
					}
					System.out.println(nodeIp.getName() + "==========" + nodeIp.getText() + "=====Table====" + (String) objects[10] + "======Filed=====" + (String) objects[11]);

					String nodeTextVal = nodeIp.getText().toString();
					// 判断字符串是否为空串 空串直接跳过 不处理
					if("".equals(nodeTextVal))
					{
						continue;//跳过本次
					}
					// 使用反射，调用对象实体类Set方法
					DrgsIndex.setElementValue(mrIpInfo, objects[4].toString(), objects[13] == null ? "" : objects[13].toString(),objects[12].toString(), nodeTextVal,objects[5] == null ? "" : objects[5].toString(),null);
				}
				if (mrIpInfo.getP00230001() == null && mrIpInfo.getP00230000() != null) {// 入院途径方式
					String inWay = mrIpInfo.getP00230000();// 入院途径方式
					inWay =  dictionaryDataMap.get("P00230000"+"#"+inWay) == null ? null : dictionaryDataMap.get("P00230000"+"#"+inWay).toString() ;
					mrIpInfo.setP00230001(inWay);
				}

				if (mrIpInfo.getP00620001() == null && mrIpInfo.getP00620000() != null) {// 离院方式
					String outWay = mrIpInfo.getP00620000();// 离院方式
					outWay =  dictionaryDataMap.get("P00620000"+"#"+outWay) == null ? null : dictionaryDataMap.get("P00620000"+"#"+outWay).toString() ;
					mrIpInfo.setP00620001(outWay);
				}
				fileUploadFacade.parseDepDoc(mrBaseInfo,mrIpInfo,paramMap);
			}
			
			// 附页信息
			List<Object[]> appendInfoXmlStrucList = xmlStructureApplication.getXmlStructureByParam("medicalRecord","MR_APPENDPAGE", "", "");
			if (!appendInfoXmlStrucList.isEmpty()) {
				for (Object[] objects : appendInfoXmlStrucList) {
					String xpathAppend = pathPrefix + objects[3].toString().replaceFirst(curNodeName,curNodeName+num);
					xmlPathInfo = xpathAppend;
					System.out.println("附页信息xml路径为：" + xpathAppend);
					Node nodeAppend = element.selectSingleNode(xpathAppend);
					if(nodeAppend==null)
					{
						continue;//当前节点不存在 跳过
					}
					System.out.println(nodeAppend.getName() + "==========" + nodeAppend.getText() + "=====Table====" + (String) objects[10] + "======Filed=====" + (String) objects[11]);
					String nodeTextVal = nodeAppend.getText().toString();
					// 判断字符串是否为空串 空串直接跳过 不处理
					if("".equals(nodeTextVal))
					{
						continue;//跳过本次
					}
					// 使用反射，调用对象实体类Set方法
					DrgsIndex.setElementValue(mrAppendInfo, objects[4].toString(), objects[13] == null ? "" : objects[13].toString(),objects[12].toString(), nodeTextVal,objects[5] == null ? "" : objects[5].toString(),null);
				}
			}
			//设置主键  优先级 ->系统外部传入 ->指定具体字段->病案号+出院日期+住院次数 
			if(paramMap.get("PMAININDEX")!=null)
			{
				pMainIndex = paramMap.get("PMAININDEX").toString();
			}
			else if(StringUtils.hasLength(mrBaseInfo.getPmainIndex()))
			{
				pMainIndex = mrBaseInfo.getPmainIndex();
			}
			else {
				if(mrBaseInfo.getP00050000()!=null)
				{
					mrNo = mrBaseInfo.getP00050000();
				}
				if(mrIpInfo.getP00290000()!=null)
				{
					mrOutDate = new SimpleDateFormat("yyyy-MM-dd").format(mrIpInfo.getP00290000());
				}
				if(mrBaseInfo.getP00040000()!=null)
				{
					mrInHospCount = mrBaseInfo.getP00040000()+"";
				}
				pMainIndex = mrNo + "-" + mrOutDate + "-" + mrInHospCount;
			}
				

			/**
			 * 主要诊断信息
			 */
			int diagIndex = 0;
			String diagMainPath = "";// 记录主要诊断路径
			List<Object[]> diagnoseMainXmlStrucList = xmlStructureApplication.getXmlStructureByParam("medicalRecord","MR_DIAGNOSE", "M","");
			if (!diagnoseMainXmlStrucList.isEmpty()) {
				// 确定哪个节点下是主要诊断
				String xpath = (diagnoseMainXmlStrucList.get(0))[3].toString();
				diagMainPath = pathPrefix + xpath.replaceFirst(curNodeName,curNodeName+num);// 记录主要诊断路径
				// 诊断信息
				MrDiagnose mr_diag_main = new MrDiagnose();
				// 获取主要诊断
				List<Object[]> mainDiagnoseXmlStrucList = xmlStructureApplication.getXmlStructureByXpath("MR_DIAGNOSE", xpath+"/");
				if (!mainDiagnoseXmlStrucList.isEmpty()) {
					for (Object[] objects : mainDiagnoseXmlStrucList) {
						String xpathMainDia = pathPrefix + objects[3].toString().replaceFirst(curNodeName,curNodeName+num);// 获取主要诊断xml路径
						xmlPathInfo = xpathMainDia;
						Node nodeMainDia = element.selectSingleNode(xpathMainDia);
						if(nodeMainDia==null)
						{
							continue;//当前节点不存在 跳过
						}
						System.out.println("主要诊断：" + nodeMainDia.getName() + "==========" + nodeMainDia.getText() + "=====Table====" + (String) objects[10] + "======Filed=====" + (String) objects[11]);
						// 使用反射，调用对象实体类Set方法
						String nodeTextVal = nodeMainDia.getText().toString();
						// 判断字符串是否为空串 空串直接跳过 不处理
						if("".equals(nodeTextVal))
						{
							continue;//跳过本次
						}
						DrgsIndex.setElementValue(mr_diag_main, objects[4].toString(),objects[13] == null ? "" : objects[13].toString(), objects[12].toString(),nodeTextVal, objects[5] == null ? "" : objects[5].toString(),null);
					}
					if(StringUtils.hasLength(mr_diag_main.getPd0101()))
					{//诊断编码不为空 保存
						mr_diag_main.setPmainIndex(pMainIndex);
						mr_diag_main.setPdType("M");
						mr_diag_main.setPdIndex(diagIndex++);
						diagList.add(mr_diag_main);
					}
				}
			}

			/**
			 * 其它诊断
			 */
			List<Object[]> diagnoseOtherXmlStrucList = xmlStructureApplication.getXmlStructureByParam("medicalRecord","MR_DIAGNOSE", "O", "");
			if (!diagnoseOtherXmlStrucList.isEmpty()) {
				// 其它诊断的节点名称相同
				if (1 == diagnoseOtherXmlStrucList.size()) {
					String xpath = (diagnoseOtherXmlStrucList.get(0))[3].toString();//其他诊断xpath
					// 获取其它诊断路径
					String pathOtherDia = pathPrefix + xpath.replaceFirst(curNodeName,curNodeName+num);
					// 获取其它诊断子节点结构
					List<Object[]> otherDiagnoseXmlStrucList = xmlStructureApplication.getXmlStructureByXpath("MR_DIAGNOSE",xpath+"/");
					// 获取其它诊断节点
					Node nodeOtherDia = element.selectSingleNode(pathOtherDia);
					if(nodeOtherDia!=null)
					{//其他诊断节点存在
						// 获取其它诊断的父节点
						Node nodeOthDiaParent = nodeOtherDia.selectSingleNode("..");
						// 获取父节点下与其它诊断节点名称相同的节点个数
						List<Node> nodeChild = nodeOthDiaParent.selectNodes(nodeOtherDia.getName());
						if (nodeChild.size() > 0) {
							// 没有以]结尾，说明父节点(nodeOthDiaParent)下全是其它诊断,从第一个往后逐一获取
							if (!pathOtherDia.endsWith("]")) {
								String otherDiaXpath = pathOtherDia;
								// 循环获取其它诊断
								for (int i = 1; i <= nodeChild.size(); i++) {
									// 诊断信息
									MrDiagnose mr_diag_other = new MrDiagnose();
									// 循环其它诊断具体的节点，取值并保存
									for (Object[] objects : otherDiagnoseXmlStrucList) {
										// 获取其它诊断路径
										String pathOtherDiaNode = objects[3].toString().replace(xpath,"");//把路径的前半段截取掉
										otherDiaXpath = pathOtherDia + "[" + i + "]" + pathOtherDiaNode;
										xmlPathInfo = otherDiaXpath;
										// 获取其它诊断节点
										Node otherDiaNode = element.selectSingleNode(otherDiaXpath);
										if(otherDiaNode==null)
										{
											continue;//当前节点不存在 跳过
										}
										System.out.println("其它诊断1：" + otherDiaNode.getName() + "==========" + otherDiaNode.getText() + "=====Table====" + (String) objects[10] + "======Filed=====" + (String) objects[11]);
										// 使用反射，调用对象实体类Set方法
										String nodeTextVal = otherDiaNode.getText().toString();
										// 判断字符串是否为空串 空串直接跳过 不处理
										if("".equals(nodeTextVal))
										{
											continue;//跳过本次
										}
										DrgsIndex.setElementValue(mr_diag_other, objects[4].toString(),objects[13] == null ? "" : objects[13].toString(), objects[12].toString(),nodeTextVal,objects[5] == null ? "" : objects[5].toString(),null);
									}
									if(StringUtils.hasLength(mr_diag_other.getPd0101()))
									{//诊断编码不为空 保存
										mr_diag_other.setPmainIndex(pMainIndex);
										mr_diag_other.setPdType("O");
										mr_diag_other.setPdIndex(diagIndex++);
										diagList.add(mr_diag_other);
									}
									otherDiaXpath = pathOtherDia;
								}
							} else {// 没有以]结尾，说明父节点(nodeOthDiaParent)下第一个是主要诊断，后面全是其它诊断，从第二个往后逐一获取
								String otherDiaXpath2 = pathOtherDia.substring(0, pathOtherDia.lastIndexOf("["));
								String otherDiaXpath3 = otherDiaXpath2;
								if (diagMainPath.endsWith("]")) {
									diagMainPath = diagMainPath.substring(0, diagMainPath.lastIndexOf("["));
								}
								int i = 1;// 默认从第一个开始
								if (otherDiaXpath3.equals(diagMainPath)) {
									i = 2;
								} // 当主诊断和其他诊断的路径一致时从第二个开始
								
								// 循环获取其它诊断
								for (; i <= nodeChild.size(); i++) {
									// 诊断信息
									MrDiagnose mr_diag_other = new MrDiagnose();
									// 循环其它诊断具体的节点，取值并保存
									for (Object[] objects : otherDiagnoseXmlStrucList) {
										// 获取其它诊断路径
										String pathOtherDiaNode = objects[3].toString().replace(xpath,"");//把路径的前半段截取掉
										// 路径从2开始累加
										otherDiaXpath3 = otherDiaXpath2 + "[" + i + "]" + pathOtherDiaNode;
										xmlPathInfo = otherDiaXpath3;
										// 获取其它诊断节点
										Node otherDiaNode = element.selectSingleNode(otherDiaXpath3);
										if(otherDiaNode==null)
										{
											continue;//当前节点不存在 跳过
										}
										System.out.println("其它诊断2：" + otherDiaNode.getName() + "==========" + otherDiaNode.getText() + "=====Table====" + (String) objects[10] + "======Filed=====" + (String) objects[11]);
										// 使用反射，调用对象实体类Set方法
										String nodeTextVal = otherDiaNode.getText().toString();
										// 判断字符串是否为空串 空串直接跳过 不处理
										if("".equals(nodeTextVal))
										{
											continue;//跳过本次
										}
										DrgsIndex.setElementValue(mr_diag_other, objects[4].toString(),objects[13] == null ? "" : objects[13].toString(), objects[12].toString(),nodeTextVal,objects[5] == null ? "" : objects[5].toString(),null);
									}
									if(StringUtils.hasLength(mr_diag_other.getPd0101()))
									{//诊断编码不为空 保存
										mr_diag_other.setPmainIndex(pMainIndex);
										mr_diag_other.setPdType("O");
										mr_diag_other.setPdIndex(diagIndex++);
										diagList.add(mr_diag_other);
									}
									otherDiaXpath3 = otherDiaXpath2;
								}
							}
						}
					}
				} else {// 其它诊断的节点名称不同
					// 逐一循环其它诊断节点
					for (Object[] objects : diagnoseOtherXmlStrucList) {
						String xpath = objects[3].toString();//其他诊断xpath
						// 获取其它诊断子节点结构
						List<Object[]> otherDiagnoseXmlStrucList = xmlStructureApplication.getXmlStructureByXpath("MR_DIAGNOSE", xpath+"/");
						if (!otherDiagnoseXmlStrucList.isEmpty()) {
							// 诊断信息
							MrDiagnose mr_diag_other = new MrDiagnose();

							for (Object[] objectsOther : otherDiagnoseXmlStrucList) {
								String xpathOtherDia = pathPrefix + objectsOther[3].toString().replaceFirst(curNodeName,curNodeName+num);// 获取其它诊断xml路径
								xmlPathInfo = xpathOtherDia;
								Node nodeOtherDia = element.selectSingleNode(xpathOtherDia);
								if(nodeOtherDia==null)
								{
									continue;//当前节点不存在 跳过
								}
								System.out.println("其他诊断3：" + nodeOtherDia.getName() + "==========" + nodeOtherDia.getText() + "=====Table====" + (String) objectsOther[10] + "======Filed=====" + (String) objectsOther[11]);
								// 使用反射，调用对象实体类Set方法
								String nodeTextVal = nodeOtherDia.getText().toString();
								// 判断字符串是否为空串 空串直接跳过 不处理
								if("".equals(nodeTextVal))
								{
									continue;//跳过本次
								}
								DrgsIndex.setElementValue(mr_diag_other, objects[4].toString(),objects[13] == null ? "" : objects[13].toString(), objects[12].toString(),nodeTextVal, objects[5] == null ? "" : objects[5].toString(),null);
							}
							if(StringUtils.hasLength(mr_diag_other.getPd0101()))
							{//诊断编码不为空 保存
								mr_diag_other.setPmainIndex(pMainIndex);
								mr_diag_other.setPdType("O");
								mr_diag_other.setPdIndex(diagIndex++);
								diagList.add(mr_diag_other);
							}
						}
					}
				}
			}

			/**
			 * 主要手术
			 */
			int operIndex = 0;
			String operMainPath = "";// 记录主要手术路径
			List<Object[]> operationMainXmlStrucList = xmlStructureApplication.getXmlStructureByParam("medicalRecord","MR_OPERATION", "M","");
			if (!operationMainXmlStrucList.isEmpty()) {
				// 确定哪个节点下是主要手术
				String xpath = (operationMainXmlStrucList.get(0))[3].toString();//主要手术xpath节点
				operMainPath = pathPrefix + xpath.replaceFirst(curNodeName,curNodeName+num);// 记录主要手术路径
				// 手术信息
				MrOperation mr_oper_main = new MrOperation();
				// 获取主要手术
				List<Object[]> mainOperationXmlStrucList = xmlStructureApplication.getXmlStructureByXpath("MR_OPERATION",xpath+"/");
				if (!mainOperationXmlStrucList.isEmpty()) {
					for (Object[] objects : mainOperationXmlStrucList) {
						String xpathMainOper = pathPrefix + objects[3].toString().replaceFirst(curNodeName,curNodeName+num);// 获取主要手术xml路径
						xmlPathInfo = xpathMainOper;
						Node nodeMainOper = element.selectSingleNode(xpathMainOper);
						if(nodeMainOper==null)
						{
							continue;//当前节点不存在 跳过
						}
						System.out.println("主要手术：" + nodeMainOper.getName() + "==========" + nodeMainOper.getText() + "=====Table====" + (String) objects[10] + "======Filed=====" + (String) objects[11]);
						// 使用反射，调用对象实体类Set方法
						String nodeTextVal = nodeMainOper.getText().toString();
						// 判断字符串是否为空串 空串直接跳过 不处理
						if("".equals(nodeTextVal))
						{
							continue;//跳过本次
						}
						DrgsIndex.setElementValue(mr_oper_main, objects[4].toString(),objects[13] == null ? "" : objects[13].toString(), objects[12].toString(),nodeTextVal, objects[5] == null ? "" : objects[5].toString(),null);
					}
					if(StringUtils.hasLength(mr_oper_main.getPo0101()))
					{//手术编码不为空时 才进行存储
						mr_oper_main.setPmainIndex(pMainIndex);
						mr_oper_main.setPoType("M");
						mr_oper_main.setPoIndex(operIndex++);
						if(!StringUtils.hasLength(mr_oper_main.getPo0301())&&StringUtils.hasLength(mr_oper_main.getPo0300()))
						{//手术及操作i级别编码为空 手术及操作i级别不为空
							String levelDesc = mr_oper_main.getPo0300();//手术及操作i级别
							String levelCode =  dictionaryDataMap.get("PO0301"+"#"+levelDesc) == null ? null : dictionaryDataMap.get("PO0301"+"#"+levelDesc).toString() ;
							mr_oper_main.setPo0301(levelCode);//手术及操作i级别编码
						}
						//麻醉方式
						if(!StringUtils.hasLength(mr_oper_main.getPo0701())&&StringUtils.hasLength(mr_oper_main.getPo0700()))
						{//麻醉方式编码为空 麻醉方式不为空
							String anaesWay = mr_oper_main.getPo0700();//麻醉方式
							String anaesCode =  dictionaryDataMap.get("PO0701"+"#"+anaesWay) == null ? null : dictionaryDataMap.get("PO0701"+"#"+anaesWay).toString() ;
							mr_oper_main.setPo0701(anaesCode);//麻醉方式编码
						}
						//切口愈合
						if(!StringUtils.hasLength(mr_oper_main.getPo0801())&&StringUtils.hasLength(mr_oper_main.getPo0800()))
						{//切口愈合编码为空 切口愈合不为空
							String cutLev = mr_oper_main.getPo0800();//切口愈合等级
							String cutLevCode =  dictionaryDataMap.get("PO0801"+"#"+cutLev) == null ? null : dictionaryDataMap.get("PO0801"+"#"+cutLev).toString() ;
							mr_oper_main.setPo0801(cutLevCode);//切口愈合等级编码
						}
						operList.add(mr_oper_main);
					}
				}
			}
			else
			{//手术节点主要其他通过 节点值区分  ---适用于卫统4格式的xml
				//按照级别降序排列获取所有节点
				List<Object[]> operationXmlStrucList = xmlStructureApplication.getXmlStructureOrderByLD("medicalRecord","MR_OPERATION");
				Map<String,Object> filedMap = new HashMap<String,Object>();
				filedMap.put("POTYPE","poType");
				if(!operationXmlStrucList.isEmpty())
				{
					//1级重复节点在 list 中的位置 0
					//2级重复节点在 list 中的位置 i
					int sp = 0,i=0;
					for (Object[] objects : operationXmlStrucList) {
						if(i>0&&"1".equals(objects[7].toString()))
						{
							sp = i;
							break;
						}
						i++;
					}
					//第一级 重复节点
					String flp = operationXmlStrucList.get(0)[3].toString();//first level path 第一级节点
					String flps = flp;//  first level path shot 第一级节点 如果末尾是[i]
					if(flps.endsWith("]"))
					{
						flps = flps.substring(0,flps.lastIndexOf("["));
					}
					String flpr = pathPrefix + flps.replaceFirst(curNodeName,curNodeName+num);// first level path real 第一级节点 真实路径
					
					// 获取手术一级节点
					Node nodeOper = element.selectSingleNode(flpr);
					if(nodeOper!=null)
					{
						// 获取手术一级节点的父节点
						Node nodeOperParent = nodeOper.selectSingleNode("..");
						// 获取手术一级节点 节点个数
						List<Node> nodeChild = nodeOperParent.selectNodes(nodeOper.getName());
						for(int n=0;n<nodeChild.size();n++)
						{
							//获取二级节点
							String slp = operationXmlStrucList.get(sp)[3].toString();//second level path 第二级节点
							String slps = slp;//second level path shot 第二级节点
							
							if(slps.endsWith("]"))
							{
								slps = slps.substring(0,slps.lastIndexOf("["));
							}
							String slpi = slps.replace(flp,flps+"["+(n+1)+"]");//second level path i  第 i个手术 第二级节点
							String slpr = pathPrefix + slpi.replaceFirst(curNodeName,curNodeName+num);// 获取主要手术xml路径
							// 获取手术二级节点
							Node nodeOperS = element.selectSingleNode(slpr);
							if(nodeOperS!=null)
							{
								// 获取手术二级节点的父节点
								Node nodeOperSParent = nodeOperS.selectSingleNode("..");
								// 获取手术二级节点 节点个数
								List<Node> nodeChildS = nodeOperSParent.selectNodes(nodeOperS.getName());
								for(int m=0;m<nodeChildS.size();m++)
								{
									//新建一个手术
									MrOperation operation = new MrOperation();
									//对二级的每个字段进行循环
									for(int k = sp+1;k<operationXmlStrucList.size();k++)
									{
										String slfp = operationXmlStrucList.get(k)[3].toString();//获取第二级要重复的点
										slfp = slfp.replace(slp,slps+"["+(m+1)+"]");//替换二级节点中的序号
										slfp = slfp.replace(flp,flps+"["+(n+1)+"]");//替换一级节点中的序号
										slfp = pathPrefix + slfp.replaceFirst(curNodeName,curNodeName+num);// 二级节点字段的真正路径
										xmlPathInfo = slfp;
										Node nodeOperSF = element.selectSingleNode(slfp);//二级节点字段值
										if(nodeOperSF==null)
										{
											continue;//当前节点不存在 跳过
										}
										System.out.println("手术：" + nodeOperSF.getName() + "==========" + nodeOperSF.getText() + "=====Table====" + (String) operationXmlStrucList.get(k)[10] + "======Filed=====" + (String) operationXmlStrucList.get(k)[11]);
										// 使用反射，调用对象实体类Set方法
										String nodeTextVal = nodeOperSF.getText().toString();
										// 判断字符串是否为空串 空串直接跳过 不处理
										if("".equals(nodeTextVal))
										{
											continue;//跳过本次
										}
										// 手术类型 主要  其他
										if ("POTYPE".equals(operationXmlStrucList.get(k)[12].toString())) {
											if("1".equals(nodeTextVal))
											{
												nodeTextVal = "M";
											}
											else
											{
												nodeTextVal = "O";
											}
										}
										// 是否手术 1 手术 0 操作
										if ("PO1400".equals(operationXmlStrucList.get(k)[12].toString())) {
											if("1".equals(nodeTextVal))
											{
												nodeTextVal = "1";
											}
											else
											{
												nodeTextVal = "0";
											}
										}
										DrgsIndex.setElementValue(operation, operationXmlStrucList.get(k)[4].toString(),operationXmlStrucList.get(k)[13] == null ? "" : operationXmlStrucList.get(k)[13].toString(), operationXmlStrucList.get(k)[12].toString(),nodeTextVal, operationXmlStrucList.get(k)[5] == null ? "" : operationXmlStrucList.get(k)[5].toString(),filedMap);
									}
									//对一级节点进行赋值
									for(int b = 1;b<sp;b++)
									{
										String flfp = operationXmlStrucList.get(b)[3].toString();//获取第一级要重复的点
										flfp = flfp.replace(flp,flps+"["+(n+1)+"]");//替换一级节点中的序号
										flfp = pathPrefix + flfp.replaceFirst(curNodeName,curNodeName+num);//一级节点字段的真正路径
										xmlPathInfo = flfp;
										Node nodeOperFF = element.selectSingleNode(flfp);//一级节点字段值
										if(nodeOperFF==null)
										{
											continue;//当前节点不存在 跳过
										}
										System.out.println("手术：" + nodeOperFF.getName() + "==========" + nodeOperFF.getText() + "=====Table====" + (String) operationXmlStrucList.get(b)[10] + "======Filed=====" + (String) operationXmlStrucList.get(b)[11]);
										// 使用反射，调用对象实体类Set方法
										String nodeTextVal = nodeOperFF.getText().toString();
										// 判断字符串是否为空串 空串直接跳过 不处理
										if("".equals(nodeTextVal))
										{
											continue;//跳过本次
										}
										DrgsIndex.setElementValue(operation, operationXmlStrucList.get(b)[4].toString(),operationXmlStrucList.get(b)[13] == null ? "" : operationXmlStrucList.get(b)[13].toString(), operationXmlStrucList.get(b)[12].toString(),nodeTextVal, operationXmlStrucList.get(b)[5] == null ? "" : operationXmlStrucList.get(b)[5].toString(),null);
									}
									if(StringUtils.hasLength(operation.getPo0101()))
									{//手术编码不为空时 才进行存储
										operation.setPmainIndex(pMainIndex);
										operation.setPoIndex(operIndex++);
										//麻醉方式
										if(!StringUtils.hasLength(operation.getPo0701())&&StringUtils.hasLength(operation.getPo0700()))
										{//麻醉方式编码为空 麻醉方式不为空
											String anaesWay = operation.getPo0700();//麻醉方式
											String anaesCode =  dictionaryDataMap.get("PO0701"+"#"+anaesWay) == null ? null : dictionaryDataMap.get("PO0701"+"#"+anaesWay).toString() ;
											operation.setPo0701(anaesCode);//麻醉方式编码
										}
										//切口愈合
										if(!StringUtils.hasLength(operation.getPo0801())&&StringUtils.hasLength(operation.getPo0800()))
										{//切口愈合编码为空 切口愈合不为空
											String cutLev = operation.getPo0800();//切口愈合等级
											String cutLevCode =  dictionaryDataMap.get("PO0801"+"#"+cutLev) == null ? null : dictionaryDataMap.get("PO0801"+"#"+cutLev).toString() ;
											operation.setPo0801(cutLevCode);//切口愈合等级编码
										}
										operList.add(operation);
									}
								}
							}
						}	
					}
				}
			}

			/**
			 * 其它手术
			 */
			List<Object[]> operationOtherXmlStrucList = xmlStructureApplication.getXmlStructureByParam("medicalRecord","MR_OPERATION", "O","");
			if (!operationOtherXmlStrucList.isEmpty()) {
				// 其它手术的节点名称相同
				if (1 == operationOtherXmlStrucList.size()) {
					// 获取其它手术节点 xpath
					String xpath = (operationOtherXmlStrucList.get(0))[3].toString();
					// 获取其它手术路径
					String pathOtherOper = pathPrefix + xpath.replaceFirst(curNodeName,curNodeName+num);
					
					// 获取其它手术子节点结构
					List<Object[]> otherOperationXmlStrucList = xmlStructureApplication.getXmlStructureByXpath("MR_OPERATION", xpath+"/");

					// 获取其它手术节点
					Node nodeOtherOper = element.selectSingleNode(pathOtherOper);
					if(nodeOtherOper!=null)
					{//当前节点存在
						// 获取其它手术的父节点
						Node nodeOthOperParent = nodeOtherOper.selectSingleNode("..");
						// 获取父节点下与其它手术节点名称相同的节点个数
						List<Node> nodeChild = nodeOthOperParent.selectNodes(nodeOtherOper.getName());

						if (nodeChild.size() > 0) {
							// 没有以]结尾，说明父节点(nodeOthDiaParent)下全是其它手术,从第一个往后逐一获取
							if (!pathOtherOper.endsWith("]")) {
								String otherOperXpath = pathOtherOper;
								// 循环获取其它手术
								for (int i = 1; i <= nodeChild.size(); i++) {
									// 手术信息
									MrOperation mr_oper_other = new MrOperation();

									// 循环其它手术具体的节点，取值并保存
									for (Object[] objects : otherOperationXmlStrucList) {
										// 获取其它手术路径
										String pathOtherOperNode = objects[3].toString().replace(xpath,"");//把路径的前半段截取掉
										otherOperXpath = pathOtherOper + "[" + i + "]" + pathOtherOperNode;
										xmlPathInfo = otherOperXpath;
										// 获取其它手术节点
										Node otherOperNode = element.selectSingleNode(otherOperXpath);
										if(otherOperNode==null)
										{
											continue;//当前节点不存在 跳过
										}
										System.out.println("其它手术1：" + otherOperNode.getName() + "==========" + otherOperNode.getText() + "=====Table====" + (String) objects[10] + "======Filed=====" + (String) objects[11]);
										// 使用反射，调用对象实体类Set方法
										String nodeTextVal = otherOperNode.getText().toString();
										// 判断字符串是否为空串 空串直接跳过 不处理
										if("".equals(nodeTextVal))
										{
											continue;//跳过本次
										}
										DrgsIndex.setElementValue(mr_oper_other, objects[4].toString(),objects[13] == null ? "" : objects[13].toString(), objects[12].toString(),nodeTextVal,objects[5] == null ? "" : objects[5].toString(),null);
									}
									if(StringUtils.hasLength(mr_oper_other.getPo0101()))
									{//有手术编码时才存储
										mr_oper_other.setPmainIndex(pMainIndex);
										mr_oper_other.setPoType("O");
										mr_oper_other.setPoIndex(operIndex++);
										if(!StringUtils.hasLength(mr_oper_other.getPo0301())&&StringUtils.hasLength(mr_oper_other.getPo0300()))
										{//手术及操作i级别编码为空 手术及操作i级别不为空
											if(!StringUtils.hasLength(mr_oper_other.getPo0301())&&StringUtils.hasLength(mr_oper_other.getPo0300()))
											{//手术及操作i级别编码为空 手术及操作i级别不为空
												String levelDesc = mr_oper_other.getPo0300();//手术及操作i级别
												String levelCode =  dictionaryDataMap.get("PO0301"+"#"+levelDesc) == null ? null : dictionaryDataMap.get("PO0301"+"#"+levelDesc).toString() ;
												mr_oper_other.setPo0301(levelCode);//手术及操作i级别编码
											}
											//麻醉方式
											if(!StringUtils.hasLength(mr_oper_other.getPo0701())&&StringUtils.hasLength(mr_oper_other.getPo0700()))
											{//麻醉方式编码为空 麻醉方式不为空
												String anaesWay = mr_oper_other.getPo0700();//麻醉方式
												String anaesCode =  dictionaryDataMap.get("PO0701"+"#"+anaesWay) == null ? null : dictionaryDataMap.get("PO0701"+"#"+anaesWay).toString() ;
												mr_oper_other.setPo0701(anaesCode);//麻醉方式编码
											}
											//切口愈合
											if(!StringUtils.hasLength(mr_oper_other.getPo0801())&&StringUtils.hasLength(mr_oper_other.getPo0800()))
											{//切口愈合编码为空 切口愈合不为空
												String cutLev = mr_oper_other.getPo0800();//切口愈合等级
												String cutLevCode =  dictionaryDataMap.get("PO0801"+"#"+cutLev) == null ? null : dictionaryDataMap.get("PO0801"+"#"+cutLev).toString() ;
												mr_oper_other.setPo0801(cutLevCode);//切口愈合等级编码
											}
										}
										operList.add(mr_oper_other);
									}
									otherOperXpath = pathOtherOper;
								}
							} else {// 以]结尾，说明父节点(nodeOthDiaParent)下第一个是主要手术，后面全是其它手术，从第二个往后逐一获取
								String otherOperXpath2 = pathOtherOper.substring(0, pathOtherOper.lastIndexOf("["));
								if (operMainPath.endsWith("]")) {
									operMainPath = operMainPath.substring(0, operMainPath.lastIndexOf("["));
								}
								String otherOperXpath3 = otherOperXpath2;
								int i = 1;// 默认从第一个开始
								if (otherOperXpath3.equals(operMainPath)) {
									i = 2;
								} // 当主手术和其他手术的路径一致时从第二个开始
								// 循环获取其它手术
								for (; i <= nodeChild.size(); i++) {
									// 手术信息
									MrOperation mr_oper_other = new MrOperation();
									// 循环其它手术具体的节点，取值并保存
									for (Object[] objects : otherOperationXmlStrucList) {
										// 获取其它手术路径
										String pathOtherOperNode = objects[3].toString().replace(xpath,"");//把路径的前半段截取掉
										// 路径从2开始累加
										otherOperXpath3 = otherOperXpath2 + "[" + i + "]" + pathOtherOperNode;
										xmlPathInfo = otherOperXpath3;
										// 获取其它手术节点
										Node otherOperNode = element.selectSingleNode(otherOperXpath3);
										if(otherOperNode==null)
										{
											continue;//当前节点不存在 跳过
										}
										System.out.println("其它手术2：" + otherOperNode.getName() + "==========" + otherOperNode.getText() + "=====Table====" + (String) objects[10] + "======Filed=====" + (String) objects[11]);
										// 使用反射，调用对象实体类Set方法
										String nodeTextVal = otherOperNode.getText().toString();
										// 判断字符串是否为空串 空串直接跳过 不处理
										if("".equals(nodeTextVal))
										{
											continue;//跳过本次
										}
										DrgsIndex.setElementValue(mr_oper_other, objects[4].toString(),objects[13] == null ? "" : objects[13].toString(),objects[12].toString(),nodeTextVal, objects[5] == null ? "" : objects[5].toString(),null);
									}

									if(StringUtils.hasLength(mr_oper_other.getPo0101()))
									{//手术编码不为空才存储
										mr_oper_other.setPmainIndex(pMainIndex);
										mr_oper_other.setPoType("O");
										mr_oper_other.setPoIndex(operIndex++);
										if(!StringUtils.hasLength(mr_oper_other.getPo0301())&&StringUtils.hasLength(mr_oper_other.getPo0300()))
										{//手术及操作i级别编码 手术及操作i级别不为空
											if(!StringUtils.hasLength(mr_oper_other.getPo0301())&&StringUtils.hasLength(mr_oper_other.getPo0300()))
											{//手术及操作i级别编码为空 手术及操作i级别不为空
												if(!StringUtils.hasLength(mr_oper_other.getPo0301())&&StringUtils.hasLength(mr_oper_other.getPo0300()))
												{//手术及操作i级别编码为空 手术及操作i级别不为空
													String levelDesc = mr_oper_other.getPo0300();//手术及操作i级别
													String levelCode =  dictionaryDataMap.get("PO0301"+"#"+levelDesc) == null ? null : dictionaryDataMap.get("PO0301"+"#"+levelDesc).toString() ;
													mr_oper_other.setPo0301(levelCode);//手术及操作i级别编码
												}
												//麻醉方式
												if(!StringUtils.hasLength(mr_oper_other.getPo0701())&&StringUtils.hasLength(mr_oper_other.getPo0700()))
												{//麻醉方式编码为空 麻醉方式不为空
													String anaesWay = mr_oper_other.getPo0700();//麻醉方式
													String anaesCode =  dictionaryDataMap.get("PO0701"+"#"+anaesWay) == null ? null : dictionaryDataMap.get("PO0701"+"#"+anaesWay).toString() ;
													mr_oper_other.setPo0701(anaesCode);//麻醉方式编码
												}
												//切口愈合
												if(!StringUtils.hasLength(mr_oper_other.getPo0801())&&StringUtils.hasLength(mr_oper_other.getPo0800()))
												{//切口愈合编码为空 切口愈合不为空
													String cutLev = mr_oper_other.getPo0800();//切口愈合等级
													String cutLevCode =  dictionaryDataMap.get("PO0801"+"#"+cutLev) == null ? null : dictionaryDataMap.get("PO0801"+"#"+cutLev).toString() ;
													mr_oper_other.setPo0801(cutLevCode);//切口愈合等级编码
												}
											}
										}
										operList.add(mr_oper_other);
									}
									otherOperXpath3 = otherOperXpath2;
								}
							}
						}
					}
				} else {// 其它手术的节点名称不同
					// 逐一循环其它手术节点
					for (Object[] objects : operationOtherXmlStrucList) {
						String xpath = objects[3].toString();
						// 获取其它手术子节点结构
						List<Object[]> otherOperationXmlStrucList = xmlStructureApplication.getXmlStructureByXpath("MR_OPERATION", xpath);
						if (!otherOperationXmlStrucList.isEmpty()) {
							// 手术信息
							MrOperation mr_oper_other = new MrOperation();

							for (Object[] objectsOther : otherOperationXmlStrucList) {
								String xpathOtherOper = pathPrefix + objectsOther[3].toString().replaceFirst(curNodeName,curNodeName+num);// 获取其它手术xml路径
								xmlPathInfo = xpathOtherOper;
								Node nodeOtherOper = element.selectSingleNode(xpathOtherOper);
								if(nodeOtherOper==null)
								{
									continue;//当前节点不存在 跳过
								}
								System.out.println("其他手术3：" + nodeOtherOper.getName() + "==========" + nodeOtherOper.getText() + "=====Table====" + (String) objectsOther[10] + "======Filed=====" + (String) objectsOther[11]);
								// 使用反射，调用对象实体类Set方法
								String nodeTextVal = nodeOtherOper.getText().toString();
								// 判断字符串是否为空串 空串直接跳过 不处理
								if("".equals(nodeTextVal))
								{
									continue;//跳过本次
								}
								DrgsIndex.setElementValue(mr_oper_other, objects[4].toString(),objects[13] == null ? "" : objects[13].toString(), objects[12].toString(),nodeTextVal,objects[5] == null ? "" : objects[5].toString(),null);
							}
							if(StringUtils.hasLength(mr_oper_other.getPo0101()))
							{//手术编码不为空才存储
								mr_oper_other.setPmainIndex(pMainIndex);
								mr_oper_other.setPoType("O");
								mr_oper_other.setPoIndex(operIndex++);
								if(!StringUtils.hasLength(mr_oper_other.getPo0301())&&StringUtils.hasLength(mr_oper_other.getPo0300()))
								{//手术及操作i级别编码 手术及操作i级别不为空
									if(!StringUtils.hasLength(mr_oper_other.getPo0301())&&StringUtils.hasLength(mr_oper_other.getPo0300()))
									{//手术及操作i级别编码为空 手术及操作i级别不为空
										if(!StringUtils.hasLength(mr_oper_other.getPo0301())&&StringUtils.hasLength(mr_oper_other.getPo0300()))
										{//手术及操作i级别编码为空 手术及操作i级别不为空
											String levelDesc = mr_oper_other.getPo0300();//手术及操作i级别
											String levelCode =  dictionaryDataMap.get("PO0301"+"#"+levelDesc) == null ? null : dictionaryDataMap.get("PO0301"+"#"+levelDesc).toString() ;
											mr_oper_other.setPo0301(levelCode);//手术及操作i级别编码
										}
										//麻醉方式
										if(!StringUtils.hasLength(mr_oper_other.getPo0701())&&StringUtils.hasLength(mr_oper_other.getPo0700()))
										{//麻醉方式编码为空 麻醉方式不为空
											String anaesWay = mr_oper_other.getPo0700();//麻醉方式
											String anaesCode =  dictionaryDataMap.get("PO0701"+"#"+anaesWay) == null ? null : dictionaryDataMap.get("PO0701"+"#"+anaesWay).toString() ;
											mr_oper_other.setPo0701(anaesCode);//麻醉方式编码
										}
										//切口愈合
										if(!StringUtils.hasLength(mr_oper_other.getPo0801())&&StringUtils.hasLength(mr_oper_other.getPo0800()))
										{//切口愈合编码为空 切口愈合不为空
											String cutLev = mr_oper_other.getPo0800();//切口愈合等级
											String cutLevCode =  dictionaryDataMap.get("PO0801"+"#"+cutLev) == null ? null : dictionaryDataMap.get("PO0801"+"#"+cutLev).toString() ;
											mr_oper_other.setPo0801(cutLevCode);//切口愈合等级编码
										}
									}
								}
								operList.add(mr_oper_other);
							}
						}
					}
				}
			}

			/**
			 * 费用信息
			 */
			List<Object[]> mrCostXmlStrucList = xmlStructureApplication.getXmlStructureByParam("medicalRecord","T_COSTSUB", "", "");
			
			if (!mrCostXmlStrucList.isEmpty()) {
				int expIndex = 0;
				double drugCost = 0;
				double materCost = 0;
				double drgMedCost = 0;
				double drgNursCost = 0;
				double drgTechCost = 0;
				double drgDrugCost = 0;
				double drgManageCost = 0;
				Map<String, String> codeMap = new HashMap<String, String>();
				if (mrCostXmlStrucList.size() == 1) {// 节点重复
					String xpath = pathPrefix + mrCostXmlStrucList.get(0)[3].toString().replaceFirst(curNodeName,curNodeName+num);// 当前节点的xpath
					String npName = mrCostXmlStrucList.get(0)[15].toString();// 节点或者属性名
					String valueType = mrCostXmlStrucList.get(0)[17].toString();// 值类型
					String feeValueFeild = xpath.substring(xpath.lastIndexOf("/"), xpath.length());// 费用值字段
					String parentXpath = xpath.substring(0, xpath.lastIndexOf("/"));// 父级路径
					if(parentXpath.endsWith("]"))//如果以[i]结尾 把[i]截掉
					{
						parentXpath = parentXpath.substring(0, parentXpath.lastIndexOf("["));
					}
					String mapPosition = mrCostXmlStrucList.get(0)[14].toString();// 映射位置
					if ("c".equals(valueType)) {// 代码--找映射关系
						InputStream propInputStream = XmlStructureFacadeImpl.class.getResourceAsStream("/costCodeMap.properties");
						Properties properties = new Properties();
						try {
							properties.load(propInputStream);
							Iterator<String> it = properties.stringPropertyNames().iterator();
							while (it.hasNext()) {
								String key = it.next();
								codeMap.put(key, properties.getProperty(key));
							}
						} catch (IOException e) {
							logger.error(e.getMessage());
							e.printStackTrace();
						} finally {
							try {
								propInputStream.close();
							} catch (IOException e) {
								logger.error(e.getMessage());
								e.printStackTrace();
							}
						}
					} else {// 描述--拿着描述去找别名
						List<Map<String,Object>> cList = costSubApplication.getAllCostSub();
						for (int i = 0; i < cList.size(); i++) {
							codeMap.put(cList.get(i).get("CS_ALIAS").toString(), cList.get(i).get("COST_SUB_CODE").toString());
						}
					}
					if ("n".equals(mapPosition)) {// 映射位置是节点值
						Node nodeCost = element.selectSingleNode(parentXpath);// --cost节点
						if(nodeCost!=null)
						{//节点存在
							// 获取其父节点
							Node nodeCostParent = nodeCost.selectSingleNode("..");
							// 获取父节点下与其它手术节点名称相同的节点个数
							List<Node> nodeChild = nodeCostParent.selectNodes(nodeCost.getName());
							for (int i = 1; i <= nodeChild.size(); i++) {
								// 费用信息
								MrExpense mr_expense = new MrExpense();
								String valueXpath = parentXpath + "[" + i + "]" + feeValueFeild;
								xmlPathInfo = valueXpath;
								String codeXpath = parentXpath + "[" + i + "]" + "/" + npName;
								if(element.selectSingleNode(valueXpath)==null||element.selectSingleNode(codeXpath)==null)
								{
									continue;
								}
								String value = element.selectSingleNode(valueXpath).getText();
								if("".equals(value)||!value.matches("^(\\-|\\+)?(\\d+\\.\\d+|\\d+|\\.\\d+)$"))
								{
									continue;
								}
								String code = element.selectSingleNode(codeXpath).getText();
								code = codeMap.get(code);// 对代码做一次映射
								double tmpC = Double.parseDouble(value);//费用值
								String cateCode = ((Map<String,CostSub>)paramMap.get("mapCostSub")).get(code).getParcostCatecode();//费用类型  1药品 2材料
								String dccCode = ((Map<String,CostSub>)paramMap.get("mapCostSub")).get(code).getDccCode();//费用五大类  
								if("1".equals(cateCode))
								{
									drugCost += tmpC; 
								}
								else if("2".equals(cateCode))
								{
									materCost += tmpC; 
								}
								if("med".equals(dccCode))
								{
									drgMedCost += tmpC;
								}
								else if("nurs".equals(dccCode))
								{
									drgNursCost += tmpC;
								}
								else if("tech".equals(dccCode))
								{
									drgTechCost += tmpC;
								}
								else if("manag".equals(dccCode))
								{
									drgManageCost += tmpC;
								}
								else if("drug".equals(dccCode))
								{
									drgDrugCost += tmpC;
								}
								mr_expense.setPe0101(code);// 费用代码
								mr_expense.setPe0200(tmpC);// 费用值
								mr_expense.setPeIndex(expIndex++);//序号码
								mr_expense.setPmainIndex(pMainIndex);// 病例外键
								expList.add(mr_expense);
							}
							expenseSum.setPmainIndex(pMainIndex);
							expenseSum.setDrugCost(drugCost);
							expenseSum.setMaterCost(materCost);
							expenseSum.setDrgDrugCost(drgDrugCost);
							expenseSum.setDrgManageCost(drgManageCost);
							expenseSum.setDrgMedCost(drgMedCost);
							expenseSum.setDrgNursCost(drgNursCost);
							expenseSum.setDrgTechCost(drgTechCost);
						}
					} else {// 映射位置是属性值
						String propertyPosition = mrCostXmlStrucList.get(0)[16].toString();// 属性的位置
						if ("p".equals(propertyPosition)) {// 当前节点的父节点
							Node nodeCost = element.selectSingleNode(parentXpath);// --cost节点
							if(nodeCost!=null)
							{//节点存在
								// 获取其父节点
								Node nodeCostParent = nodeCost.selectSingleNode("..");
								// 获取父节点下与其它手术节点名称相同的节点个数
								List<Node> nodeChild = nodeCostParent.selectNodes(nodeCost.getName());
								for (int i = 1; i <= nodeChild.size(); i++) {
									// 费用信息
									MrExpense mr_expense = new MrExpense();
									String valueXpath = parentXpath + "[" + i + "]" + feeValueFeild;
									xmlPathInfo = valueXpath;
									String propertyXpath = parentXpath + "[" + i + "]";
									if(element.selectSingleNode(valueXpath)==null||element.selectSingleNode(propertyXpath)==null)
									{
										continue;
									}
									String value = element.selectSingleNode(valueXpath).getText();
									if("".equals(value)||!value.matches("^(\\-|\\+)?(\\d+\\.\\d+|\\d+|\\.\\d+)$"))
									{
										continue;
									}
									DefaultElement defaultElement = (DefaultElement) element.selectSingleNode(propertyXpath);
									String property = defaultElement.attributeValue(npName);
									property = codeMap.get(property);// 对代码做一次映射
									double tmpC = Double.parseDouble(value);//费用值
									String cateCode = ((Map<String,CostSub>)paramMap.get("mapCostSub")).get(property).getParcostCatecode();//费用类型  1药品 2材料
									String dccCode = ((Map<String,CostSub>)paramMap.get("mapCostSub")).get(property).getDccCode();//费用五大类  
									if("1".equals(cateCode))
									{
										drugCost += tmpC; 
									}
									else if("2".equals(cateCode))
									{
										materCost += tmpC; 
									}
									if("med".equals(dccCode))
									{
										drgMedCost += tmpC;
									}
									else if("nurs".equals(dccCode))
									{
										drgNursCost += tmpC;
									}
									else if("tech".equals(dccCode))
									{
										drgTechCost += tmpC;
									}
									else if("manag".equals(dccCode))
									{
										drgManageCost += tmpC;
									}
									else if("drug".equals(dccCode))
									{
										drgDrugCost += tmpC;
									}
									mr_expense.setPe0101(property);// 费用代码
									mr_expense.setPe0200(tmpC);// 费用值
									mr_expense.setPeIndex(expIndex++);//序号码
									mr_expense.setPmainIndex(pMainIndex);// 病例外键
									expList.add(mr_expense);
								}
								expenseSum.setPmainIndex(pMainIndex);
								expenseSum.setDrugCost(drugCost);
								expenseSum.setMaterCost(materCost);
								expenseSum.setDrgDrugCost(drgDrugCost);
								expenseSum.setDrgManageCost(drgManageCost);
								expenseSum.setDrgMedCost(drgMedCost);
								expenseSum.setDrgNursCost(drgNursCost);
								expenseSum.setDrgTechCost(drgTechCost);
							}
						} else {// 当前节点的同级节点
							Node nodeCost = element.selectSingleNode(xpath);// --cost节点
							if(nodeCost!=null)
							{//节点存在
								// 获取其父节点
								Node nodeCostParent = nodeCost.selectSingleNode("..");
								// 获取父节点下与其它手术节点名称相同的节点个数
								List<Node> nodeChild = nodeCostParent.selectNodes(nodeCost.getName());
								for (int i = 1; i <= nodeChild.size(); i++) {
									// 费用信息
									MrExpense mr_expense = new MrExpense();
									String valueXpath = xpath + "[" + i + "]";
									xmlPathInfo = valueXpath;
									if(element.selectSingleNode(valueXpath)==null)
									{
										continue;
									}
									String value = element.selectSingleNode(valueXpath).getText();
									if("".equals(value)||!value.matches("^(\\-|\\+)?(\\d+\\.\\d+|\\d+|\\.\\d+)$"))
									{
										continue;
									}
									DefaultElement defaultElement = (DefaultElement) element.selectSingleNode(valueXpath);
									String property = defaultElement.attributeValue(npName);
									property = codeMap.get(property);// 对代码做一次映射
									double tmpC = Double.parseDouble(value);//费用值
									mr_expense.setPe0101(property);// 费用代码
									mr_expense.setPe0200(tmpC);// 费用值
									mr_expense.setPeIndex(expIndex++);//序号码
									mr_expense.setPmainIndex(pMainIndex);// 病例外键
									expList.add(mr_expense);
								}
							}
						}
					}
				} else {// 节点不重复--依次循环每个节点
					for (int i = 0; i < mrCostXmlStrucList.size(); i++) {
						String xpath = pathPrefix + mrCostXmlStrucList.get(i)[3].toString().replaceFirst(curNodeName,curNodeName+num);
						xmlPathInfo = xpath;
						if(element.selectSingleNode(xpath)==null)
						{
							continue;
						}
						String value = element.selectSingleNode(xpath).getText();// 费用值
						if("".equals(value)||!value.matches("^(\\-|\\+)?(\\d+\\.\\d+|\\d+|\\.\\d+)$"))
						{
							continue;
						}
						String code = mrCostXmlStrucList.get(i)[12].toString();// 费用编码
						double tmpC = Double.parseDouble(value);//费用值
						String cateCode = ((Map<String,CostSub>)paramMap.get("mapCostSub")).get(code).getParcostCatecode();//费用类型  1药品 2材料
						String dccCode = ((Map<String,CostSub>)paramMap.get("mapCostSub")).get(code).getDccCode();//费用五大类  
						if("1".equals(cateCode))
						{
							drugCost += tmpC; 
						}
						else if("2".equals(cateCode))
						{
							materCost += tmpC; 
						}
						if("med".equals(dccCode))
						{
							drgMedCost += tmpC;
						}
						else if("nurs".equals(dccCode))
						{
							drgNursCost += tmpC;
						}
						else if("tech".equals(dccCode))
						{
							drgTechCost += tmpC;
						}
						else if("manag".equals(dccCode))
						{
							drgManageCost += tmpC;
						}
						else if("drug".equals(dccCode))
						{
							drgDrugCost += tmpC;
						}
						MrExpense mr_expense = new MrExpense();
						mr_expense.setPe0101(code);// 费用名称
						mr_expense.setPe0200(tmpC);// 费用值
						mr_expense.setPeIndex(expIndex++);//序号码
						mr_expense.setPmainIndex(pMainIndex);// 病例外键
						expList.add(mr_expense);
					}
					expenseSum.setPmainIndex(pMainIndex);
					expenseSum.setDrugCost(drugCost);
					expenseSum.setMaterCost(materCost);
					expenseSum.setDrgDrugCost(drgDrugCost);
					expenseSum.setDrgManageCost(drgManageCost);
					expenseSum.setDrgMedCost(drgMedCost);
					expenseSum.setDrgNursCost(drgNursCost);
					expenseSum.setDrgTechCost(drgTechCost);
				}
			}
			mrBaseInfo.setPmainIndex(pMainIndex);
			mrIpInfo.setPmainIndex(pMainIndex);
			mrAppendInfo.setPmainIndex(pMainIndex);
			caseMap.put("pMainIndex",pMainIndex);
			caseMap.put("base",mrBaseInfo);
			caseMap.put("ip",mrIpInfo);
			caseMap.put("append",mrAppendInfo);
			caseMap.put("diag",diagList);
			caseMap.put("oper",operList);
			caseMap.put("exp",expList);
			caseMap.put("expsum",expenseSum);
			fileUploadApplication.saveCaseInfo(caseMap);
			fileUploadFacade.assembleAndGroup(caseMap,paramMap);
		} catch (Exception e) {
			e.printStackTrace();
			//写入log日志
			logger.error(xmlPathInfo+ " " +e.getMessage());
		}
		
	}
    
    /**
     * 
     * 获取xml配置表信息 
     * 
     * @author ShaoZhiYuan  
     * @see com.dhcc.DRGsHosp.facade.XmlStructureFacade#findXMLTable()
     */
	@Override
	public List<Map<String, Object>> findXMLTable(String templateType) {
		List<Object[]> list=xmlStructureApplication.findXMLTable(templateType);
		//将数据组装成需要的格式
		List<Map<String, Object>> resultList=new LinkedList<>();
		//序列号
		for(Object[] obj: list){
			Map<String, Object> map=new HashMap<>();
			map.put("nodeName", obj[0]==null?"":obj[0].toString().trim());
			map.put("nodeLevel", obj[1]==null?"":obj[1].toString().trim());
			map.put("dataType", obj[2]==null?"":obj[2].toString().trim());
			map.put("dataFormat", obj[3]==null?"":obj[3].toString().trim());
			map.put("tableCode", obj[4]==null?"":obj[4].toString().trim());
			map.put("tableDesc", obj[5]==null?"":obj[5].toString().trim());
			map.put("fieldCode", obj[6]==null?"":obj[6].toString().trim());
			map.put("fieldDesc", obj[7]==null?"":obj[7].toString().trim());
			map.put("nodeId", obj[8]==null?"":obj[8].toString().trim());
			map.put("parentId", obj[9]==null?"":obj[9].toString().trim());
			map.put("pType", obj[10]==null?"":obj[10].toString().trim());
			resultList.add(map);
		}
		return resultList;
	}

	@Override
	public List<Map<String,Object>> findNodeConfigInfoByPath(String path) {
		String pathCode = path.hashCode()+"" ;
		return xmlStructureApplication.findNodeConfigInfoByPath(pathCode) ;
	}
}
